// Test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "CObj.h"
#include "CScene.h"
#include "CMateriales.h"

#define MOVE_STEP .4
#define ANGLE_STEP 0.2
#define PI 3.1416f

float g_x=0.0f;
float g_y=0.0f;
float g_z=10.0f;
float g_roll= 0.0f;
float g_pitch= 0.0f;
float g_yaw= 0.0f;
int g_w;
int g_h;
float g_cubeAngle= 0.f;
int g_camara=0;
CScene* escena = new CScene();
int luces[4];
bool g_l1=true;
bool g_l2=false;
bool g_l3=true;
bool g_l4=false;

void Keyboard(unsigned char key,int x, int y)
{
	//keyboard callback function
	switch (key)
	{
		case '1': g_camara = 0;
				  escena->cambiarCamara(g_camara);						
				break;
		case '2': g_camara = 1;
				  escena->cambiarCamara(g_camara);						
				break;
		case '3': g_camara = 2;
				  escena->cambiarCamara(g_camara);						
				break;
		case '4': g_camara = 3;
				  escena->cambiarCamara(g_camara);						
				break;
		case '5': g_l1 = !(g_l1);
				if (g_l1)
					escena->setLuz(0);
				else
					glDisable(GL_LIGHT0);
			break;
		case '6': g_l2 = !(g_l2);
				if(g_l2)
					escena->setLuz(1);
				else
					glDisable(GL_LIGHT1);
			break;
		case '7': g_l3 = !(g_l3);
				if(g_l3)
					escena->setLuz(2);
				else
					glDisable(GL_LIGHT2);
			break;
		case '8': g_l4 = !(g_l4);
				if(g_l4)
					escena->setLuz(3);
				else
					glDisable(GL_LIGHT3);
			break;
		case 27: exit(0);
	}
}

void Set3DView()
{
	//set projection matrix
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);

	//set modelview matrix
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity ();
	glRotatef(-g_roll, 0.0, 0.0, 1.0);	
	glRotatef(-g_yaw, 0.0, 1.0, 0.0);
	glRotatef(-g_pitch, 1.0, 0.0, 0.0);	
	glTranslatef(-g_x, -g_y, -g_z);
	escena->dibujaCamara();
}



//void DrawCube()
//{
//	glColor3f (0.5, 1.0, 0.5);
//	glMatrixMode(GL_MODELVIEW);
//	
//	glRotatef(g_cubeAngle,1.0,0.0,0.0);
//	glutWireCube (1.0);
//}

void DrawScene(void)
{
	//clean the backbuffer
	glClear (GL_COLOR_BUFFER_BIT);

	//viewing transformation
	Set3DView();

	//draw the cube
	//DrawCube();	
}

void Reshape (int w, int h)
{
	//Reshape callback function
	g_h= h;
	g_w= w;
	glViewport (0, 0, (GLsizei) w, (GLsizei) h);
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
}

int main(int argc, char** argv)
{

	//INIT GLUT/////////////////////
	////////////////////////////////
	//init window and OpenGL context
	glutInit(&argc, argv);
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize (1024, 768); 
	glutCreateWindow (argv[0]);
//	glutFullScreen();


	//callback functions
	glutDisplayFunc(DrawScene);
	glutReshapeFunc(Reshape);
	glutKeyboardFunc(Keyboard);

	//LLAMADAS AL LOADFILE DE TODOS PARA PODER PROBARLO
	CObj* objetoPrueba = new CObj();
	objetoPrueba->loadFile("models/batman.obj");	
	escena->loadFile("escena.txt");
	CMateriales* objMateriales = new CMateriales();
	objMateriales->loadFile("models/batman.mtl");
	//objMateriales->setMaterial("wire_174186203");

	while (1)
	{
		//UPDATE////////////////////
		////////////////////////////
		//"move" the cube
	//	g_cubeAngle+= 0.1;
		//queued events?
		glutMainLoopEvent();


		//RENDER////////////////////
		////////////////////////////
		glutPostRedisplay();
		glutSwapBuffers();
	}
   return 0;
}